package com.example.myapplication.tack_30;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.utils.OkHttpTool;
import com.example.myapplication.utils.StateData;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

public class AddAddressActivity extends AppCompatActivity {
    private ImageView exit;
    private EditText addressDetail;
    private RadioGroup radioGroup;
    private EditText name;
    private EditText phone;
    private TextView add;
    private JSONObject jsonObject;
//    <!--    "addressDetail": "大连理工大学教学楼 A3-118",
//"label": "学校",
//"name": "王大卫",
//"phone": "15898135276"-->
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_32_activity_add_address);
        init();
        initExit();

        cilckAdd();
    }

    private void cilckAdd() {
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();
                RadioButton radioButton = findViewById(checkedRadioButtonId);
                String s = radioButton.getText().toString();
                String s1 = addressDetail.getText().toString();
                String s2 = name.getText().toString();
                String s3 = phone.getText().toString();

                try {
                    jsonObject.put("addressDetail",s1);
                    jsonObject.put("label",s);
                    jsonObject.put("name",s2);
                    jsonObject.put("phone",s3);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = jsonObject.toString();
                addAddress(json);
            }
        });
    }

    private void addAddress(String json) {
        String url = "prod-api/api/takeout/address";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, StateData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                StateData stateData = (StateData)obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(AddAddressActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();
                        if(stateData.getCode()==200){
                            AddAddressActivity.this.finish();
                        }
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }

    private void initExit() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddAddressActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        addressDetail = findViewById(R.id.addressDetail);
        radioGroup = findViewById(R.id.radio_group);
        name = findViewById(R.id.name);
        phone = findViewById(R.id.phone);

        add = findViewById(R.id.add);
        jsonObject = new JSONObject();
    }
}